#!/usr/bin/ruby -Ku
# -*- coding: utf-8 -*-
require 'test/unit'
require 'm3g'

class Test_Camera < Test::Unit::TestCase
  def setup
  end

  def teadown
  end

  def test_defualt_variables
    cam   = M3G::Camera.new;
    trans = M3G::Transform.new

    assert_equal M3G::Camera::GENERIC, cam.projection.type
    proj  = [1,0,0,0,
             0,1,0,0,
             0,0,1,0,
             0,0,0,1]
    for i in 0..15
      assert_equal proj[i], cam.projection.transform.matrix[i]
    end
  end

  def test_set_variables_parallel
    cam   = M3G::Camera.new
    trans = M3G::Transform.new

    cam.parallel = [10, 1.5, 1, 100]
    
    assert_equal M3G::Camera::PARALLEL, cam.projection.type
    assert_equal 10 , cam.projection.params[0]
    assert_equal 1.5, cam.projection.params[1]
    assert_equal 1  , cam.projection.params[2]
    assert_equal 100, cam.projection.params[3]

    mat = [0.13333333,0,0,0, 0,0.200000000,0,0, 0,0,-0.020202,-1.0202, 0,0,0,1]
    for i in 0..15
      assert_in_delta mat[i], cam.projection.transform.matrix[i], 0.00001
    end
  end

  def test_set_variables_perspective
    cam   = M3G::Camera.new
    trans = M3G::Transform.new

    cam.perspective = [60, 1.5, 1, 100]
    
    assert_equal M3G::Camera::PERSPECTIVE, cam.projection.type
    assert_equal 60 , cam.projection.params[0]
    assert_equal 1.5, cam.projection.params[1]
    assert_equal 1  , cam.projection.params[2]
    assert_equal 100, cam.projection.params[3]

    mat = [1.1547,0,0,0, 0,1.73205,0,0, 0,0,-1.0202,-2.0202, 0,0,-1,0]
    for i in 0..15
      assert_in_delta mat[i], cam.projection.transform.matrix[i], 0.00001
    end
  end

  def test_set_variables_generic
    cam   = M3G::Camera.new
    trans = M3G::Transform.new
    trans.matrix = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

    cam.generic = trans

    # GENERICの時は.paramsは未定義

    assert_equal M3G::Camera::GENERIC, cam.projection.type
    for i in 0..15
      assert_equal i, cam.projection.transform.matrix[i]
    end
  end
  
  def test_duplicate
    cam1   = M3G::Camera.new
    trans = M3G::Transform.new
    trans.matrix = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]

    cam1.generic = trans

    cam2 = cam1.duplicate

    assert_equal M3G::Camera::GENERIC, cam2.projection.type
    mat = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
    for i in 0..15
      assert_equal mat[i], cam2.projection.transform.matrix[i]
    end
  end

  # この関数はM3G未定義
  def test_look_at
    cam = M3G::Camera.new 
    cam.look_at 1,2,3, -3,-3,-1, 3,3,1

    mat = [0.63375,0.563612,0.529813,1, -0.724286,0.191868,0.662266,2, 0.271607,-0.803447,0.529813,3, 0,0,0,1]
    for i in 0..15
      assert_in_delta mat[i], cam.transform.matrix[i], 0.00001
    end
    
  end

end
